<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
//Includes
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class WO_delete_frame extends Page
{
       public $hiddenWO = null;
       public $hiddenVeh = null;
       function WO_delete_frameCreate($sender, $params)
       {
       $veh = $_GET['veh'];
       $WO = $_GET['WO'];
       $this->hiddenVeh->setValue($veh);
       $this->hiddenWO->setValue($WO);

       //for trouble
       $query_sel_wo = "SELECT trouble_ids FROM veh_workorder_tbl WHERE workorder_id='".$this->hiddenWO->getValue()."'";
       $result_sel_wo = mysql_query($query_sel_wo) or die("SELECT WO ".mysql_error());
       $row_sel_wo = mysql_fetch_array($result_sel_wo);

       $trouble_ids = $row_sel_wo['trouble_ids'];
       if($trouble_ids!='')
       {
        $trouble_ids = explode(',',$trouble_ids);
        foreach($trouble_ids as $t_id)
        {
         $query_up_trouble = "UPDATE veh_trouble_tbl SET flag_used=0 WHERE autoid='".$t_id."'";
         mysql_query($query_up_trouble) or die("UPDATE USED FLAG ".mysql_error());
        }
       }

        $query_delete_wo = "DELETE FROM veh_workorder_tbl WHERE workorder_id='".$this->hiddenWO->getValue()."'";
        mysql_query($query_delete_wo) or die("DELETE WO ".mysql_error());

        $query_delete_laborline = "DELETE FROM veh_laborline_tbl WHERE workorder='".$this->hiddenWO->getValue()."'";
        mysql_query($query_delete_laborline) or die("DELETE LABORLINE ".mysql_error());

        $query_sel_parts = "SELECT parts_id,quantity,part_num FROM veh_parts_tbl WHERE workorder='".$this->hiddenWO->getValue()."'";
        $result_sel_parts = mysql_query($query_sel_parts) or die("SELECT PARTS ".mysql_error());
        while($row_sel_parts = mysql_fetch_array($result_sel_parts))
        {
         $part_id = $row_sel_parts['parts_id'];
         $qty_delete = $row_sel_parts['quantity'];
         $stock_id = $row_sel_parts['part_num'];

         $query_inv = "SELECT qty_available FROM invstocks_tbl WHERE stock_id='".$stock_id."'";
         $result_inv = mysql_query($query_inv) or die('INVENTORY - QUANTITY '.mysql_error());
         $row_inv = mysql_fetch_array($result_inv);
         $curr_qty = $row_inv['qty_available'];

         $new_qty = $curr_qty + $qty_delete;
         $query_up_inv = "UPDATE invstocks_tbl SET qty_available='".$new_qty."' WHERE stock_id='".$stock_id."'";
         mysql_query($query_up_inv) or die("UPDATE INV ".mysql_error());

         $query_select_inv_stat = "SELECT qty_available FROM invstocks_tbl WHERE stock_id='".$stock_id."'";
         $result_select_inv_stat = mysql_query($query_select_inv_stat) or die("SELECT INV STAT ".mysql_error());
         $count_inv_stat = mysql_num_rows($result_select_inv_stat);

         if($count_inv_stat>0)
         {
          $query_up_inv = "UPDATE invstocks_status_tbl SET qty_available='".$new_qty."' WHERE stock_id='".$stock_id."'";
          mysql_query($query_up_inv) or die("UPDATE INV STATUS ".mysql_error());
         }

         $query_delete_parts = "DELETE FROM veh_parts_tbl WHERE parts_id ='".$part_id."'";
         mysql_query($query_delete_parts) or die("DELETE PARTS ".mysql_error());
        }

       $result = mysql_query("delete from veh_workorder_tbl where vehicle_id='".$this->hiddenVeh->getValue()."' and workorder_id='".$this->hiddenWO->getValue()."'");
       redirect('veh_workorder_list_frame.php?veh='.$this->hiddenVeh->getValue().'&WO='.$this->hiddenWO->getValue());


       }

}

global $application;

global $WO_delete_frame;

//Creates the form
$WO_delete_frame=new WO_delete_frame($application);

//Read from resource file
$WO_delete_frame->loadResource(__FILE__);

//Shows the form
$WO_delete_frame->show();

?>